package com.base.cn.platform.os.controller.op.article;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.article.OpArticle;
import com.base.cn.platform.os.service.op.article.OpArticleBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 资讯Controller
 * @author zsw
 * @create 2018/5/7
 */
@RestController
@RequestMapping("/manage/op")
public class OpArticleController extends BaseController {

    @Autowired
    private OpArticleBiz opArticleBiz;

    /**
     * 查询资讯列表
     * @param currentPage 当前页
     * @param article 资讯实体
     * @return
     */
    @RequestMapping("/queryArticleList")
    public PageInfo<OpArticle> queryArticleList(@RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                                @RequestBody OpArticle article) {
        Pagination articlePage = new Pagination();
        articlePage.setCurrentPage(currentPage);
        PageInfo<OpArticle> articlePageInfo = opArticleBiz.queryArticleList(articlePage, article,false,true);
        return articlePageInfo;
    }

    /**
     * 保存资讯
     * @param article 资讯实体
     * @return
     */
    @RequestMapping("/saveUpdateArticle")
    public Map<String, Object> saveUpdateArticle(@RequestBody OpArticle article) {
        if (ObjectUtils.isNotEmpty(article)) {
            if (StringUtils.isEmpty(article.getTitle()) || StringUtils.isEmpty(article.getTitle().trim())) {
                return ResultUtil.ERROR("资讯标题不能为空");
            }
            if (StringUtils.isEmpty(article.getAuthor())) {
                return ResultUtil.ERROR("资讯作者不能为空");
            }
            if (StringUtils.isEmpty(article.getTypeId())) {
                return ResultUtil.ERROR("资讯类型不能为空");
            }
            if (StringUtils.isEmpty(article.getKeyWord())) {
                return ResultUtil.ERROR("资讯关键字不能为空");
            }
            if (StringUtils.isEmpty(article.getClickNum())) {
                return ResultUtil.ERROR("资讯点击量不能为空");
            }
            if (!StringUtils.isNumber(article.getClickNum().toString())){
                return ResultUtil.ERROR("请正确填写点击量");
            }
            if (StringUtils.isEmpty(article.getImageJson())) {
                return ResultUtil.ERROR("资讯封面不能为空");
            }
            if (StringUtils.isEmpty(article.getBriefIntroduction())) {
                return ResultUtil.ERROR("资讯简介不能为空");
            }
            if (article.getBriefIntroduction().length() > 255) {
                return ResultUtil.ERROR("资讯简介不能超过255个字符");
            }
            if (StringUtils.isEmpty(article.getArticleContext().getContext())) {
                return ResultUtil.ERROR("填写资讯内容");
            }
            opArticleBiz.saveArticle(article);
            return ResultUtil.SUCCESS("保存成功", article);
        }
        return ResultUtil.ERROR("资讯保存失败");
    }

    /**
     * 删除资讯
     * @param articleIds 资讯id串
     * @return
     */
    @RequestMapping("/deleteArticle")
    public Map<String, Object> deleteArticle(@RequestParam("articleIds") String articleIds){
        if (ObjectUtils.isNotEmpty(articleIds)){
            opArticleBiz.deleteArticle(articleIds);
            return ResultUtil.SUCCESS("删除成功");
        }
        return ResultUtil.ERROR("删除失败");
    }

    /**
     * 根据ID查询资讯
     * @param id 资讯id
     * @param setRecommend 设置推荐
     * @param count 推荐条数
     * @return
     */
    @RequestMapping("/findArticleById")
    public OpArticle findArticleById(@RequestParam("id") BigDecimal id,
                                     @RequestParam(value = "setRecommend", required = false) boolean setRecommend,
                                     @RequestParam(value = "count", required = false, defaultValue = "5") int count){
        return opArticleBiz.findArticleById(id, setRecommend, count);
    }

    /**
     * 修改资讯状态
     * @param articleIds 资讯id串
     * @param status 状态：1.正常 2.禁用
     * @return
     */
    @RequestMapping("/updateArticleStatusByIds")
    public Map<String, Object> updateArticleStatusByIds(@RequestParam("articleIds")String articleIds,
                                                        @RequestParam("status")Integer status){
        opArticleBiz.updateArticleStatusByIds(articleIds, status);
        return ResultUtil.SUCCESS("修改成功");
    }

    /**
     * 根据Ids搜索资讯
     * @param articleIds 资讯id串
     * @param status 资讯状态
     * @param setContext 是否资讯设置内容
     * @param setType 是否设置资讯类型
     * @return
     */
    @RequestMapping("/opArticleMapByIds")
    public Map<BigDecimal,OpArticle> opArticleMapByIds(@RequestParam("articleIds")String articleIds,
                                                       @RequestParam(value = "status",required = false,defaultValue = "0") Integer status,
                                                       @RequestParam(value = "setContext",required = false,defaultValue = "false") boolean setContext,
                                                       @RequestParam(value = "setType",required = false,defaultValue = "false") boolean setType){
        return opArticleBiz.opArticleMapByIds(articleIds,status,setContext,setType);
    }

    /**
     * 查询试卷列表
     * @param article
     * @return
     */
    @RequestMapping("/findOpArticleList")
    public List<OpArticle> findOpArticleList(@RequestBody OpArticle article) {
        return opArticleBiz.findOpArticleList(article);
    }

    /**
     * 批量发布资讯
     * @param articleIds 资讯id串
     * @return
     */
    @RequestMapping("/batchPublishArticle")
    public Map<String, Object> batchPublishArticle(@RequestParam("articleIds")String articleIds){
        if(StringUtils.isNotEmpty(articleIds)){
            articleIds = opArticleBiz.getStatusConflictIds(articleIds);
            if(StringUtils.isEmpty(articleIds)){
                return ResultUtil.ERROR("所选资讯已发布，请勿重复操作");
            }
            return opArticleBiz.batchPublishArticle(articleIds);
        }
        return ResultUtil.ERROR("请选择要操作的数据记录");
    }

}
